/**
* 版权声明：厦门睿商网络科技有限公司 版权所有 违者必究
* 日    期：2020-11-11
*/
package com.rzico.picture;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.rzico.annotation.Log;
import com.rzico.base.BaseController;
import com.rzico.base.CommResult;
import com.rzico.entity.PageResult;
import com.rzico.entity.Pageable;
import com.rzico.picture.entity.Picture;
import com.rzico.picture.service.PictureService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.lang3.time.DateUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.imageio.ImageIO;
import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletResponse;
import java.awt.image.BufferedImage;
import java.io.*;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 素材管理控制层
 *
 * @author Rzico Boot
 * @version 1.0
 * @date 2020-11-11
 */
@Api(description = "素材管理接口")
@RestController
@RequestMapping("/picture")
public class PictureController extends BaseController {

    @Autowired
    private PictureService pictureService;

    /**
     * 将BufferedImage转换为InputStream
     * @param image
     * @return
     */
    public InputStream bufferedImageToInputStream(BufferedImage image){
        ByteArrayOutputStream os = new ByteArrayOutputStream();
        try {
            ImageIO.write(image, "png", os);
            InputStream input = new ByteArrayInputStream(os.toByteArray());
            return input;
        } catch (IOException e) {
            logger.error("提示:",e);
        }
        return null;
    }

    @ApiOperation("生成图片")
    @GetMapping("/showImage")
    @ApiImplicitParams({@ApiImplicitParam(name = "id", value = "素材ID", dataType = "String", paramType = "query"),
            @ApiImplicitParam(name = "isDrat", value = "是否投射(0否/1是)", dataType = "Integer", paramType = "query"),
            @ApiImplicitParam(name = "no1", value = "箱号", dataType = "String", paramType = "query"),
            @ApiImplicitParam(name = "no2", value = "封号", dataType = "String", paramType = "query"),
            @ApiImplicitParam(name = "no3", value = "柜号(前4位)", dataType = "String", paramType = "query")
    })
    public void showImage(Long id,Integer isDrat,String no1,String no2,String no3,HttpServletResponse response) {
        try {
            Picture picture = pictureService.findById(id);
            response.reset();
            response.setContentType("image/jpeg;charset=utf-8");
            ServletOutputStream output = response.getOutputStream();// 得到输出流
            try {
                FileOutputStream fileOut = null;
                HttpURLConnection conn = null;
                InputStream inputStream = null;
                try{
                    URL httpUrl=new URL(picture.getUrl());
                    conn=(HttpURLConnection) httpUrl.openConnection();
                    //以Post方式提交表单，默认get方式
                    conn.setRequestMethod("GET");
                    conn.setDoInput(true);
                    conn.setDoOutput(true);
                    // post方式不能使用缓存
                    conn.setUseCaches(false);
                    conn.connect();
                    //获取网络输入流
                    inputStream = conn.getInputStream();

                    BufferedImage srcImg = ImageIO.read(inputStream);


                    PictureUtils pictureUtils = new PictureUtils();
                    BufferedImage destImg = pictureUtils.createImage(picture, isDrat, srcImg, no1, no2, no3);


                    BufferedInputStream bis = new BufferedInputStream(bufferedImageToInputStream(destImg));
                    //判断文件的保存路径后面是否以/结尾

                    byte[] buf = new byte[4096];
                    int length = bis.read(buf);
                    //保存文件
                    while(length != -1){
                        output.write(buf, 0, length);
                        length = bis.read(buf);
                    }
                    bis.close();


                    conn.disconnect();
                } catch (Exception e){
                    e.printStackTrace();
                    System.out.println("抛出异常！！");
                }


            } catch (Exception e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
            output.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

}
